package de.velvettear.bruteforce.kernel;

import java.sql.Statement;

import de.velvettear.bruteforce.util.Log;

public class Word extends Database {
	
	public static boolean createTable() { 
		boolean created = false;
		try {			
			Statement sqlStatement = connect().createStatement();
			
			created = sqlStatement.execute("CREATE TABLE IF NOT EXISTS " + Statics.TABLE_WORDS +
														" (" +
														"m_nID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
														"m_sValue VARCHAR(1000) NOT NULL," + 
														"m_bLowercase INT(1)," + 
														"m_bUppercase INT(1)," +
														"m_bNumeric INT(1)," + 
														"m_bSpecial INT(1)," + 
														"m_bFavorite INT(1)" +
														")");		
			
			disconnect();
		} catch (Exception e) {
			Log.error(e);
		}	
		return created;
	}
		
	private static String maskSpecialCharacters(String m_sValue) {	
		m_sValue = m_sValue.replaceAll("\\\\", "\\\\\\\\");
		m_sValue = m_sValue.replaceAll("\\\'", "\\\\'");
		m_sValue = m_sValue.replaceAll("\\\"", "\\\\\"");
		return m_sValue;
	}
	
	public static boolean insert(String m_sValue) {
		boolean inserted = false;
		
		m_sValue = maskSpecialCharacters(m_sValue);
		
		try {						
			int flag_lowercase = 0;
			if (Charset.isLowercase(m_sValue)) {
				flag_lowercase = 1;
			}
			int flag_uppercase = 0;
			if (Charset.isUppercase(m_sValue)) {
				flag_uppercase = 1;
			}
			int flag_numeric = 0;
			if (Charset.isNumeric(m_sValue)) {
				flag_numeric = 1;
			}
			int flag_special = 0;
			if (Charset.isSpecial(m_sValue)) {
				flag_special = 1;
			}
			
			Statement sqlStatement = connect().createStatement();
						
			inserted = sqlStatement.execute("INSERT INTO " + Statics.TABLE_WORDS + " " +
											"(m_sValue, m_bLowercase, m_bUppercase, m_bNumeric, m_bSpecial) " +
											"VALUES " +
											"('" + m_sValue + "', " + flag_lowercase + ", " + flag_uppercase + ", " + flag_numeric + ", " + flag_special + ")");					
			
			disconnect();
		} catch (Exception e) {
			Log.error(e);
		}
		
		return inserted;	
	}
	
}
